/*
 * Copyright (c) 2023  YunLong Chen
 * Project Qing is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 */

package cn.chenyunlong.gateway.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * JWT工具类。
 */
public class JwtUtil {

    //有效期为
    public static final Long JWT_TTL = TimeUnit.HOURS.toMillis(1);
    //设置秘钥明文
    public static final String JWT_KEY = "itcast";

    /**
     * 创建token。
     *
     * @param id        TokenID
     * @param subject   主题内容
     * @param ttlMillis 过期时间
     * @return Jwt Token
     */
    public static String createJwt(String id, String subject, Long ttlMillis) {
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime expDate = now.plusNanos(ttlMillis);
        SecretKey secretKey = generalKey();
        JwtBuilder builder = Jwts.builder()
            //唯一的ID
            .setId(id)
            // 主题  可以是JSON数据
            .setSubject(subject)
            // 签发者
            .setIssuer("admin")
            // 签发时间
            .setIssuedAt(new Date())
            //使用HS256对称加密算法签名, 第二个参数为秘钥
            .signWith(signatureAlgorithm, secretKey)
            // 设置过期时间
            .setExpiration(new Date(System.currentTimeMillis() + JWT_TTL));
        return builder.compact();
    }

    /**
     * 生成密钥。
     *
     * @return 生成加密后的秘钥
     */
    public static SecretKey generalKey() {
        byte[] encodedKey = Base64.getDecoder().decode(JwtUtil.JWT_KEY);
        return new SecretKeySpec(encodedKey, 0, encodedKey.length, "AES");
    }

    /**
     * 解析JwtToken。
     *
     * @param jwt jwt信息
     * @return jwt解析内容！
     * @throws Exception 解析异常信息
     */
    public static Claims parseJwt(String jwt) throws Exception {
        SecretKey secretKey = generalKey();
        return Jwts.parser()
            .setSigningKey(secretKey)
            .parseClaimsJws(jwt)
            .getBody();
    }
}
